function Promise(executor) {
    this.PromiseState='pending';
    this.PromiseResult=null;
    this.callbacks=[];
    const that=this;
    function resolve(data){
    //状态只能修改一次，即只能从pending改为fulfilled或rejected
        if(that.PromiseState !== 'pending') return;
    //修改对象状态
        that.PromiseState='fulfilled';
    //设置对象结果值
        that.PromiseResult=data;
    //调用异步成功回调函数
        that.callbacks.forEach(item=>{
                item.onResolved(data);
        })
    }
    function reject(data){
    //状态只能修改一次
        if(that.PromiseState !== 'pending') return;
    //修改对象状态
        that.PromiseState='rejected';
    //设置对象结果值
        that.PromiseResult=data;
    //调用异步失败回调函数
    that.callbacks.forEach(item=>{
        item.onRejected(data);
})
    }
    try{//捕获异常
      executor(resolve,reject);  
    }catch(e){
        reject(e);
    }
    
}
//then方法执行回调
Promise.prototype.then=function(onResolved,onRejected){
return new Promise((resolve,reject)=>{
    if(this.PromiseState==='fulfilled'){
        let result=onResolved(this.PromiseResult);
        if(result instanceof Promise){

        }else{//非promise，结果对象状态为【成功】
            resolve(result);
        }
    }else if(this.PromiseState==='rejected'){
        onRejected(this.PromiseResult);
    }else if(this.PromiseState==='pending'){
        this.callbacks.push({//保存回调函数
            onResolved,
            onRejected,
        })
    }
    })
    
}